package com.guaju.yueshangLife.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.mine.OrderDetailActivity;
import com.guaju.yueshangLife.bean.Avatar;
import com.guaju.yueshangLife.bean.CommentsBean;
import com.guaju.yueshangLife.bean.MeOrder;
import com.guaju.yueshangLife.bean.OrderJudgeUtil;
import com.guaju.yueshangLife.databinding.FooterHomeItemBinding;
import com.guaju.yueshangLife.databinding.ItemCancledBinding;
import com.guaju.yueshangLife.databinding.ItemCommentBinding;
import com.guaju.yueshangLife.databinding.ItemUncommentBinding;
import com.guaju.yueshangLife.databinding.ItemUnpayBinding;
import com.guaju.yueshangLife.databinding.ItemUnreceivedBinding;

import java.util.List;

public class CommentsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context context;
    List<CommentsBean.DataBean> list;
    private int status = 1;

    public static final int LOAD_MORE = 0;
    public static final int LOAD_PULL_TO = 1;
    public static final int LOAD_NONE = 2;
    public static final int LOAD_END = 3;
    private static final int TYPE_FOOTER = -2;
    private static final int TYPE_CONTENT = -4;

    private int type = TYPE_CONTENT;
    Activity activity;
    private final LayoutInflater layoutInflater;
    private View view;

    public CommentsAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
        activity = (Activity) context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) {

        //选择类型
        RecyclerView.ViewHolder result = null;
        switch (type) {
            case TYPE_CONTENT:
                ItemCommentBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.item_comment, null, false);
                result = new CommentHolder(binding.getRoot());
                break;

            case TYPE_FOOTER:
                FooterHomeItemBinding mBindFooter = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.footer_home_item, null, false);
                result = new FooterViewHolder(mBindFooter.getRoot());
                break;
        }
        return result;
    }

    //绑定
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            footerViewHolder.bindItem();
        } else if (holder instanceof CommentHolder) {
            CommentHolder h = (CommentHolder) holder;
            h.setContent(list.get(position));
        }
    }

    //有多少个item？
    @Override
    public int getItemCount() {
        if (list != null && !list.isEmpty()) {
            return 1 + list.size();
        } else {
            return 0;
        }
    }

    //item类型
    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            type = TYPE_FOOTER;
        } else {

            type = TYPE_CONTENT;


        }
        return type;
    }


    public interface OnItemClickListener {
        void onItemClick(View v, int position);

        void onItemLongClick(View v);
    }

    public CommentsAdapter.OnItemClickListener mOnItemClickListener;//第二步：声明自定义的接口

    //第三步：定义方法并暴露给外面的调用者
    public void setOnItemClickListener(CommentsAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    class CommentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ItemCommentBinding binding;

        public CommentHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.getBinding(itemView);
            itemView.setOnClickListener(this);

        }

        public void setContent(CommentsBean.DataBean bean) {
            List<String> images = bean.getImages();
            String content = bean.getContent();
            int level = bean.getLevel();
            String score = bean.getScore();
            CommentsBean.DataBean.CustomerBean customer = bean.getCustomer();
            Avatar avatar = customer.getAvatar();
            String avatarPath = avatar.getHost()+"/"+avatar.getPath();
            binding.commentAvatar.setImageURI(avatarPath);
            String created_at = customer.getCreated_at();
            String name = customer.getName();
            binding.username.setText(name);
            binding.tvCommentTime.setText(created_at);
            binding.ratingbar.setRating(Float.parseFloat(score));
            binding.tvContent.setText(content);
            CommentsBean.DataBean.ProductBean product = bean.getProduct();
            List<String> attribute = product.getAttribute();
            String color = attribute.get(0);
            String size = attribute.get(1);
            binding.tvGoodsColor.setText("颜色：" + color);
            binding.tvGoodsSize.setText("尺码：" + size);
            CommentInnerAdapter commentInnerAdapter = new CommentInnerAdapter(activity, images);
            binding.recyclerViewCommentPhotos.setAdapter(commentInnerAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, 4);
            binding.recyclerViewCommentPhotos.setLayoutManager(gridLayoutManager);
        }

        @Override
        public void onClick(View v) {
        }
    }


    /**
     * footer view
     */
    private class FooterViewHolder extends RecyclerView.ViewHolder {

        FooterHomeItemBinding mBindFooter;

        FooterViewHolder(View itemView) {
            super(itemView);
            mBindFooter = DataBindingUtil.getBinding(itemView);
            mBindFooter.rlMore.setGravity(Gravity.CENTER);
        }

        private void bindItem() {
            switch (status) {
                case LOAD_MORE:
                    mBindFooter.progress.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setText("正在加载...");
                    itemView.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_PULL_TO:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("上拉加载更多");
                    itemView.setVisibility(View.VISIBLE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_NONE:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setText("暂无数据");
                    mBindFooter.tvLoadPrompt.setVisibility(View.VISIBLE);
                    mBindFooter.homeBottom.setVisibility(View.INVISIBLE);
                    break;
                case LOAD_END:
                    mBindFooter.progress.setVisibility(View.GONE);
                    mBindFooter.tvLoadPrompt.setVisibility(View.GONE);
                    mBindFooter.homeBottom.setVisibility(View.GONE);
//                    itemView.setVisibility(View.GONE);
                default:
                    break;
            }
        }

    }

    /**
     * 这里规定 position =  getItemCount() - 1 时
     * 就为尾布局，设置为占满整屏幕宽
     * getItemCount() 改了 ，这里就不用改
     */
    private boolean isFooter(int position) {
        return position < getItemCount() && position >= getItemCount() - 1;
    }

    public void updateLoadStatus(int status) {
        this.status = status;
        notifyDataSetChanged();
    }

    public int getLoadStatus() {
        return this.status;
    }

    public void setType(int type) {
        this.type = type;
    }

}